﻿
using Newtonsoft.Json;
using OpenfireAPI;
using OpenfireAPI.entity;
using OpenfireAPI.util;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QDChat.Sdk.OpenFire
{
    public class OpenFireSDK
    {
        string domain = "http://192.168.1.162";
        public void Demo()
        {
            var client = GetClient();
            //Request All available users
            var ues = client.getUsers();
            
            var ue = client.getUser("test02");            
            
            //Create new user
            bool result = client.createUser(new OpenfireAPI.entity.UserEntity() { username = "test", email = "test@gmail.com", name = "test", password = "ZAQ!2wsx" });

            Echo(result);
        }
     
        public OpenfireApiClient GetClient()
        {
            //// Shared secret key
            //OpenfireAuthenticator authenticator = new OpenfireAuthenticator("qd711688");
            //OpenfireAPIClient client = new OpenfireAPIClient(url, 9090, authenticator);


            // Shared secret key
            var authenticator = new OpenfireAuthenticator("SPk0VknTQd5iCgxH");
            var client = new OpenfireApiClient(domain, 9090, authenticator);
            return client;
        }

        public UserEntity GetUser(string username)
        {
            var client = GetClient();
            var user = client.getUser(username);
            Echo(SerializeObject(user));
            return user;
        }



        public void GetSession()
        {
            
            var client = GetClient();

            var group = client.getGroup("qidong");
            var room = client.getChatRoom("room01");

            // var session = client.getSessions();

            // Request all active Sessions from a specific user
            //restApiClient.getSessions(String username);
        }

        public void SendMsg()
        {
            var client = GetClient();
            var result = client.broadcastMsg(new MessageEntity() { body = "AAAAAA" });
        }


        public void call(string path)
        {
            var openfirePlugin = "/plugins/restapi/v1/";
            var url = openfirePlugin + path;
            var response = call(Method.GET, url, null, new Dictionary<string, string>());
            var content = response.Content;
            Echo(content);
            Echo(response.StatusCode);
        }


        private IRestResponse call(Method method, string url, object payload, Dictionary<string, string> queryParams)
        {

            var request = new RestRequest(url, method);
            foreach (KeyValuePair<string, string> entry in queryParams)
            {
                request.AddParameter(entry.Key, entry.Value);
            }
            request.RequestFormat = DataFormat.Json;
            request.AddHeader("Accept", "application/json");

            if (payload != null)
            {
                request.AddHeader("Content-Type", "application/json");
                request.JsonSerializer = new RestSharpJsonNetSerializer();
                request.AddJsonBody(payload);
            }
            var restClient = new RestClient(domain + ":9090");
            var authenticator = new OpenfireAuthenticator("SPk0VknTQd5iCgxH");
            restClient.Authenticator = authenticator;
            return restClient.Execute(request);
        }







        string SerializeObject(object obj)
        {
            if (obj == null || obj == DBNull.Value)
                return "";
            return JsonConvert.SerializeObject(obj);
        }



        void Echo(object msg)
        {
            Console.WriteLine(msg);
        }
    }
}